C语言 求方程 ax^2+bx+c=0的根

来源:百度知道 编辑:UC知道 时间:2024/06/25 15:37:48
用三个函数分别当b^2-4ac大于0,等于0,小于0时的根,并输出结果。要求从主函数输入a,b,c的值。
急急急!!!!!!!!!!!!!!!!!!
我今天就要啊!!!!!!!!!!!!!!!
C语言编程!!!!!!!!!
要通过验证的!!!!!!!!!!

很简单的
#include <iostream.h>
#include <math.h>
void f1(double a,double b,double c)
{
cout<<"一个根为"<<(-b+sqrt(b*b-4*a*c))*.5/a<<endl;
cout<<"另一个为"<<(-b-sqrt(b*b-4*a*c))*.5/a<<endl;
}
void f2(double a,double b,double c)
{
cout<<"两根相同,为"<<-b*.5/a<<endl;
}
void f3(double a,double b,double c)
{
cout<<"一个根为"<<-b*.5/a<<"+"<<sqrt(4*a*c-b*b)*.5/a<<"i"<<endl;
cout<<"一个根为"<<-b*.5/a<<"-"<<sqrt(4*a*c-b*b)*.5/a<<"i"<<endl;
}
void main(){
double a,b,c;
cout<<"以空格分隔,请分别输入a,b,c:"<<endl;
cin>>a>>b>>c;
if((b*b-4*a*c)>0)f1(a,b,c);
else if((b*b-4*a*c)<0)f3(a,b,c);
else f2(a,b,c);
}

>0时,X=-b+_根号b^2-4ac/2a